import tkinter as tk
from tkinter import messagebox
import math

root = tk.Tk()
root.title("RMSE Calculator")

tk.Label(root, text="E").grid(row=0, column=1)
tk.Label(root, text="N").grid(row=0, column=2)
tk.Label(root, text="U").grid(row=0, column=3)

tk.Label(root, text="Mean").grid(row=1, column=0)
mean_e = tk.Entry(root)
mean_e.grid(row=1, column=1)
mean_n = tk.Entry(root)
mean_n.grid(row=1, column=2)
mean_u = tk.Entry(root)
mean_u.grid(row=1, column=3)
tk.Label(root, text="Required").grid(row=1, column=4)

tk.Label(root, text="Ref.").grid(row=2, column=0)
ref_e = tk.Entry(root)
ref_e.grid(row=2, column=1)
ref_n = tk.Entry(root)
ref_n.grid(row=2, column=2)
ref_u = tk.Entry(root)
ref_u.grid(row=2, column=3)
tk.Label(root, text="Required").grid(row=2, column=4)

tk.Label(root, text="Bias").grid(row=3, column=0)
bias_e = tk.Entry(root)
bias_e.grid(row=3, column=1)
bias_n = tk.Entry(root)
bias_n.grid(row=3, column=2)
bias_u = tk.Entry(root)
bias_u.grid(row=3, column=3)


def calc_bias():
    try:
        bias_e.delete(0, tk.END)
        bias_n.delete(0, tk.END)
        bias_u.delete(0, tk.END)
        bias_e.insert(0, float(mean_e.get()) - float(ref_e.get()))
        bias_n.insert(0, float(mean_n.get()) - float(ref_n.get()))
        bias_u.insert(0, float(mean_u.get()) - float(ref_u.get()))

        bias_h.delete(0, tk.END)
        bias_v.delete(0, tk.END)
        bias_h.insert(0, math.sqrt(float(bias_e.get())**2 + float(bias_n.get())**2))
        bias_v.insert(0, float(bias_u.get()))

    except ValueError:
        messagebox.showerror("Error", "Please enter valid numbers.")


calc_bias_btn = tk.Button(root, text="Calculate Bias", command=calc_bias)
calc_bias_btn.grid(row=3, column=4)

tk.Label(root, text="H").grid(row=4, column=2)
tk.Label(root, text="V").grid(row=4, column=3)

tk.Label(root, text="Bias").grid(row=5, column=0)
bias_h = tk.Entry(root)
bias_h.grid(row=5, column=2)
bias_v = tk.Entry(root)
bias_v.grid(row=5, column=3)

tk.Label(root, text="STD").grid(row=6, column=0)
std_h = tk.Entry(root)
std_h.grid(row=6, column=2)
std_v = tk.Entry(root)
std_v.grid(row=6, column=3)
tk.Label(root, text="Required").grid(row=6, column=4)

tk.Label(root, text="RMSE").grid(row=7, column=0)
rmse_h = tk.Entry(root)
rmse_h.grid(row=7, column=2)
rmse_v = tk.Entry(root)
rmse_v.grid(row=7, column=3)


def calc_rmse():
    try:
        rmse_h.delete(0, tk.END)
        rmse_v.delete(0, tk.END)
        rmse_h.insert(0, math.sqrt((float(std_h.get())**2 + float(bias_h.get())**2)))
        rmse_v.insert(0, math.sqrt((float(std_v.get())**2 + float(bias_v.get())**2)))

    except ValueError:
        messagebox.showerror("Error", "Please enter valid numbers.")


calc_rmse_btn = tk.Button(root, text="Calculate RMSE", command=calc_rmse)
calc_rmse_btn.grid(row=7, column=4)

root.mainloop()
